﻿using DeZhen.DataCollection.IService.CollectionTask;
using DeZhen.DataCollection.Model.DTO.CollectionTask;
using Microsoft.AspNetCore.Mvc;

namespace DeZhen.DataCollection.WebApi.Controllers
{
    /// <summary>
    /// 采集任务
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CollectionTaskController : ControllerBase
    {
        private readonly ICollectionTaskService _service;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        public CollectionTaskController(ICollectionTaskService service)
        {
            _service = service;
        }

        /// <summary>
        /// 执行同步远程采集任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        [HttpPost(Name = "ExecuteSyncRemote")]
        public async Task<long?> ExecuteSyncRemoteAsync(SaveCollectionTask input)
        {
            return await _service.SaveCollectionTaskAsync(input);
        }

        /// <summary>
        /// 获取任务详情列表
        /// </summary>
        /// <param name="taskId">任务ID</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        [HttpGet(Name = "GetTaskDetailByTaskId")]
        public async Task<List<CollectionTaskDetailDTO>> GetTaskDetailByTaskIdAsync(long taskId)
        {
            if (taskId <= 0)
            {
                throw new ArgumentNullException(nameof(taskId), "任务ID不能为空");
            }
            var result = await _service.GetTaskDetailByTaskId(taskId);
            if (result == null || !result.Any())
            {
                throw new InvalidOperationException("未找到相关任务详情");
            }
            return result;
        }
    }
}
